package com.ktjy.ktmall.dao.impl;

import com.ktjy.ktmall.dao.BaseDao;
import com.ktjy.ktmall.dao.UserDao;
import com.ktjy.ktmall.entity.AddressParams;
import com.ktjy.ktmall.entity.GoodsItem;
import com.ktjy.ktmall.entity.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class UserDaoImp extends BaseDao implements UserDao {
    @Override
    public int insertUser(User user) {
        String sql = "INSERT INTO t_user(`roleId`,`loginName`,`userName`,`password`,`sex`,`identityCode`,`email`,`mobile`,`enabled`) values(?,?,?,?,?,?,?,?,?)";
        return super.executeUpdate(sql, user.getRoleId(), user.getLoginName(), user.getUserName(), user.getPassword(), user.getSex(),
                user.getIdentityCode(), user.getEmail(), user.getMobile(), user.getEnabled());
    }

    @Override
    public int updateUser(User user) {
        String sql = "UPDATE t_user set `roleId` = ?,`loginName` = ?,`userName`=?,`password`=?,`sex`=?,`identityCode`=?," +
                "`email` = ?,`mobile` = ?,`enabled` = ? where id = ? ";
        return super.executeUpdate(sql, user.getRoleId(), user.getLoginName(), user.getUserName(), user.getPassword(), user.getSex(),
                user.getIdentityCode(), user.getEmail(), user.getMobile(), user.getEnabled(), user.getId());
    }

    @Override
    public int deleteUser(User user) {
        String sql = "delete from t_user where id = ?";
        return super.executeUpdate(sql, user.getId());
    }

    @Override
    public User selectUserById(int id) {
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        Connection conn = this.getConnection();
        User a = null;
        String sql = "select * from t_user where `id` = ?";
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setObject(1, id);
            rs = pstmt.executeQuery();
            while (!rs.isClosed() && rs.next()) {
                a = new User(rs.getInt("id"),
                        rs.getInt("roleId"),
                        rs.getString("loginName"),
                        rs.getString("userName"),
                        rs.getString("password"),
                        rs.getInt("sex"),
                        rs.getString("identityCode"),
                        rs.getString("email"),
                        rs.getString("mobile"),
                        rs.getInt("enabled"),
                        rs.getString("create_time"),
                        rs.getString("update_time")
                );
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            super.closeAll(rs, pstmt, conn);
        }
        return a;
    }

    @Override
    public List<User> selectUserList() {
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        Connection conn = this.getConnection();
        List<User> userList = new ArrayList<User>();
        User a = null;
        String sql = "select * from t_user order by create_time desc ";
        try {
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            while (!rs.isClosed() && rs.next()) {
                a = new User(rs.getInt("id"),
                        rs.getInt("roleId"),
                        rs.getString("loginName"),
                        rs.getString("userName"),
                        rs.getString("password"),
                        rs.getInt("sex"),
                        rs.getString("identityCode"),
                        rs.getString("email"),
                        rs.getString("mobile"),
                        rs.getInt("enabled"),
                        rs.getString("create_time"),
                        rs.getString("update_time")
                );
                userList.add(a);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            super.closeAll(rs, pstmt, conn);
        }
        return userList;
    }

    /**
     * 分页查询用户列表信息
     *
     * @param from     起始位置偏移量
     * @param pageSize 每页显示的数据行数
     * @return 返回查询到的分页列表数据
     */
    public List<User> getGoodsListPage(Integer from, Integer pageSize) {
        List<User> list = new ArrayList<>();
        // 查询已上架的商品分页列表
        String sql = "select * from `t_user` order by create_time desc limit ?,?";
        rs = super.executeQuery(sql, from, pageSize);
        try {
            while (rs.next()) {
                User a = new User(rs.getInt("id"),
                        rs.getInt("roleId"),
                        rs.getString("loginName"),
                        rs.getString("userName"),
                        rs.getString("password"),
                        rs.getInt("sex"),
                        rs.getString("identityCode"),
                        rs.getString("email"),
                        rs.getString("mobile"),
                        rs.getInt("enabled"),
                        rs.getString("create_time"),
                        rs.getString("update_time")
                );
                list.add(a);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            super.closeAll(rs, pstmt, conn);
        }
        return list;
    }

    @Override
    public int count() {
        int count = 0;
        String sql = "select count(*) from `t_user`";
        rs = super.executeQuery(sql);
        try {
            if (rs.next()) {
                count = rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            super.closeAll(rs, pstmt, conn);
        }
        return count;
    }

    public User login(User user) {
        int count = 0;
        String sql = "select * from `t_user` where `loginName` = ? and `password` = ?";
        rs = super.executeQuery(sql,user.getLoginName(),user.getPassword());
        User u = null;
        try {
            while (rs.next()) {
                u = new User(rs.getInt("id"),
                        rs.getInt("roleId"),
                        rs.getString("loginName"),
                        rs.getString("userName"),
                        rs.getString("password"),
                        rs.getInt("sex"),
                        rs.getString("identityCode"),
                        rs.getString("email"),
                        rs.getString("mobile"),
                        rs.getInt("enabled"),
                        rs.getString("create_time"),
                        rs.getString("update_time")
                );
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            super.closeAll(rs, pstmt, conn);
        }
        return u;
    }

}
